<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=11"/>
<meta name="generator" content="Doxygen 1.10.0"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>common_util: 1. 快速指引</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<script type="text/javascript" src="clipboard.js"></script>
<script type="text/javascript" src="cookie.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr id="projectrow">
  <td id="projectalign">
   <div id="projectname">common_util
   </div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.10.0 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
var searchBox = new SearchBox("searchBox", "search/",'.html');
/* @license-end */
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(function() {
  initMenu('',true,false,'search.php','Search');
  $(function() { init_search(); });
});
/* @license-end */
</script>
<div id="main-nav"></div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<div id="MSearchResults">
<div class="SRPage">
<div id="SRIndex">
<div id="SRResults"></div>
<div class="SRStatus" id="Loading">Loading...</div>
<div class="SRStatus" id="Searching">Searching...</div>
<div class="SRStatus" id="NoMatches">No Matches</div>
</div>
</div>
</div>
</div>

</div><!-- top -->
<div><div class="header">
  <div class="headertitle"><div class="title">1. 快速指引</div></div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p><a class="anchor" id="autotoc_md45"></a> </p>
<h1><a class="anchor" id="autotoc_md46"></a>
1.1. 概述</h1>
<p>common_util是C++的一个通用工具库，采用现代C++语法（C++11及以上）实现，使用cutl作为命名空间，所有接口的命名方式与STL保持一致，可以作为STL库的一个补充，你可以像使用STL一样使用common_util。</p>
<h1><a class="anchor" id="autotoc_md47"></a>
1.2. 目录结构</h1>
<div class="fragment"><div class="line">./common_util</div>
<div class="line">├── build                       # 程序构建时的缓存目录</div>
<div class="line">├── docs                        # 文档目录</div>
<div class="line">│   ├── quick_start             # 快速指引文档</div>
<div class="line">│   └── reference               # API接口文档</div>
<div class="line">├── publish                     # 发布目录, release方式构建时会存放构建的最终结果</div>
<div class="line">│   ├── common_util             # 发布的common_util库</div>
<div class="line">│   │   ├── include             # 发布的common_util头文件目录</div>
<div class="line">│   │   └── lib                 # 发布的common_util静态库</div>
<div class="line">│   └── common_util_demo        # 发布的common_util_demo可执行程序</div>
<div class="line">├── script                      # 脚本目录</div>
<div class="line">└── src                         # 源代码目录</div>
<div class="line">    ├── common_util             # common_util源码目录</div>
<div class="line">    └── usage_demo              # common_util_demo源码目录</div>
</div><!-- fragment --><h1><a class="anchor" id="autotoc_md48"></a>
1.3. 使用方式</h1>
<p>简单的几个步骤如下：</p>
<blockquote class="doxtable">
<p>&zwj;1. 包含 <a class="el" href="common__util_8h.html" title="The index of the header files of common_util library. For convenient to use, you can only include com...">common_util.h</a> 头文件</p><ol type="1">
<li>库的初始化，注册日志打印的回调函数</li>
<li>库的使用 </li>
</ol>
</blockquote>
<p>具体样例如下：</p>
<div class="fragment"><div class="line"> ++</div>
<div class="line"><span class="preprocessor">#pragma once</span></div>
<div class="line"> </div>
<div class="line"><span class="preprocessor">#include &lt;iostream&gt;</span></div>
<div class="line"><span class="preprocessor">#include &lt;map&gt;</span></div>
<div class="line"><span class="preprocessor">#include &lt;mutex&gt;</span></div>
<div class="line"><span class="preprocessor">#include &lt;thread&gt;</span></div>
<div class="line"><span class="preprocessor">#include &quot;<a class="code" href="common__util_8h.html">common_util.h</a>&quot;</span></div>
<div class="line"> </div>
<div class="line">std::string loglevel2flag(<a class="code hl_enumeration" href="logtype_8h.html#a4a361b2bdc027c9df2fa3e539fa16959">cutl::loglevel</a> level)</div>
<div class="line">{</div>
<div class="line">    <span class="keyword">static</span> std::map&lt;cutl::loglevel, std::string&gt; levelMap = {</div>
<div class="line">        {cutl::loglevel::debug_level, <span class="stringliteral">&quot;[D]&quot;</span>},</div>
<div class="line">        {cutl::loglevel::info_level, <span class="stringliteral">&quot;[I]&quot;</span>},</div>
<div class="line">        {cutl::loglevel::warn_level, <span class="stringliteral">&quot;[W]&quot;</span>},</div>
<div class="line">        {cutl::loglevel::error_level, <span class="stringliteral">&quot;[E]&quot;</span>},</div>
<div class="line">    };</div>
<div class="line">    <span class="keyword">auto</span> itr = levelMap.find(level);</div>
<div class="line">    <span class="keywordflow">if</span> (itr != levelMap.end())</div>
<div class="line">    {</div>
<div class="line">        <span class="keywordflow">return</span> itr-&gt;second;</div>
<div class="line">    }</div>
<div class="line">    <span class="keywordflow">return</span> <span class="stringliteral">&quot;[?]&quot;</span>;</div>
<div class="line">}</div>
<div class="line"> </div>
<div class="line"><span class="keyword">static</span> std::mutex g_log_mtx_;</div>
<div class="line"><span class="keywordtype">void</span> <span class="keyword">static</span> library_log_func(<a class="code hl_enumeration" href="logtype_8h.html#a4a361b2bdc027c9df2fa3e539fa16959">cutl::loglevel</a> level, <span class="keyword">const</span> std::string &amp;msg)</div>
<div class="line">{</div>
<div class="line">    std::lock_guard&lt;std::mutex&gt; lock(g_log_mtx_);</div>
<div class="line">    <span class="keyword">auto</span> curTime = <a class="code hl_function" href="strfmt_8h.html#a003c7af3c7c683a60a11ff0cde64365f">cutl::fmt_timestamp_ms</a>(cutl::timestamp(cutl::timeunit::ms));</div>
<div class="line">    <span class="keyword">auto</span> threadId = std::this_thread::get_id();</div>
<div class="line">    <span class="keywordflow">if</span> (cutl::loglevel::error_level == level)</div>
<div class="line">    {</div>
<div class="line">        std::cerr &lt;&lt; <span class="stringliteral">&quot;[&quot;</span> &lt;&lt; curTime &lt;&lt; <span class="stringliteral">&quot;]&quot;</span> &lt;&lt; loglevel2flag(level) &lt;&lt; <span class="stringliteral">&quot;]&quot;</span> &lt;&lt; threadId &lt;&lt; <span class="stringliteral">&quot;](cutl) &quot;</span> &lt;&lt; msg &lt;&lt; std::endl;</div>
<div class="line">    }</div>
<div class="line">    <span class="keywordflow">else</span> <span class="keywordflow">if</span> (cutl::loglevel::debug_level == level)</div>
<div class="line">    {</div>
<div class="line">        <span class="comment">//  print debug log</span></div>
<div class="line">    }</div>
<div class="line">    <span class="keywordflow">else</span></div>
<div class="line">    {</div>
<div class="line">        std::cout &lt;&lt; <span class="stringliteral">&quot;[&quot;</span> &lt;&lt; curTime &lt;&lt; <span class="stringliteral">&quot;]&quot;</span> &lt;&lt; loglevel2flag(level) &lt;&lt; <span class="stringliteral">&quot;]&quot;</span> &lt;&lt; threadId &lt;&lt; <span class="stringliteral">&quot;](cutl) &quot;</span> &lt;&lt; msg &lt;&lt; std::endl;</div>
<div class="line">    }</div>
<div class="line">}</div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> *argv[])</div>
<div class="line">{</div>
<div class="line">    <span class="comment">// 库的初始化</span></div>
<div class="line">    <a class="code hl_function" href="config_8h.html#aaff727190c9cbd114cb8123f3e61e93f">cutl::library_init</a>(library_log_func);</div>
<div class="line"> </div>
<div class="line">    <span class="comment">// 数据格式化</span></div>
<div class="line">    std::cout &lt;&lt; <span class="stringliteral">&quot;fmt_uint: &quot;</span> &lt;&lt; <a class="code hl_function" href="strfmt_8h.html#a2e78cd1710a89e2f94f496bc47cd2d46">cutl::fmt_uint</a>(12, 5) &lt;&lt; std::endl;</div>
<div class="line">    std::cout &lt;&lt; <span class="stringliteral">&quot;fmt_double: &quot;</span> &lt;&lt; <a class="code hl_function" href="strfmt_8h.html#a9b0341d973ea969f0d90e55f4a1771e1">cutl::fmt_double</a>(3.141592653, 4) &lt;&lt; std::endl;</div>
<div class="line">    std::cout &lt;&lt; <span class="stringliteral">&quot;fmt_filesize: &quot;</span> &lt;&lt; <a class="code hl_function" href="strfmt_8h.html#a9a2a174ed592af8186835cd0da50a384">cutl::fmt_filesize</a>(33600) &lt;&lt; std::endl;</div>
<div class="line">    <span class="comment">// 获取系统当前时间</span></div>
<div class="line">    <span class="keyword">auto</span> now = <a class="code hl_function" href="classcutl_1_1datetime.html#a2f26f17577618f7133b62b0343ecca0c">cutl::datetime::now</a>();</div>
<div class="line">    std::cout &lt;&lt; <span class="stringliteral">&quot;current time(UTC time): &quot;</span> &lt;&lt; now.utctime() &lt;&lt; std::endl;</div>
<div class="line">    std::cout &lt;&lt; <span class="stringliteral">&quot;current time(local time): &quot;</span> &lt;&lt; now.format() &lt;&lt; std::endl;</div>
<div class="line"> </div>
<div class="line">    <span class="keywordflow">return</span> 0;</div>
<div class="line">}</div>
<div class="ttc" id="aclasscutl_1_1datetime_html_a2f26f17577618f7133b62b0343ecca0c"><div class="ttname"><a href="classcutl_1_1datetime.html#a2f26f17577618f7133b62b0343ecca0c">cutl::datetime::now</a></div><div class="ttdeci">static datetime now()</div><div class="ttdoc">Get a datetime object for the current system time.</div></div>
<div class="ttc" id="acommon__util_8h_html"><div class="ttname"><a href="common__util_8h.html">common_util.h</a></div><div class="ttdoc">The index of the header files of common_util library. For convenient to use, you can only include com...</div></div>
<div class="ttc" id="aconfig_8h_html_aaff727190c9cbd114cb8123f3e61e93f"><div class="ttname"><a href="config_8h.html#aaff727190c9cbd114cb8123f3e61e93f">cutl::library_init</a></div><div class="ttdeci">void library_init(LogFuncType log_func)</div><div class="ttdoc">Initialize the library.</div></div>
<div class="ttc" id="alogtype_8h_html_a4a361b2bdc027c9df2fa3e539fa16959"><div class="ttname"><a href="logtype_8h.html#a4a361b2bdc027c9df2fa3e539fa16959">cutl::loglevel</a></div><div class="ttdeci">loglevel</div><div class="ttdoc">The type of log level.</div><div class="ttdef"><b>Definition</b> logtype.h:31</div></div>
<div class="ttc" id="astrfmt_8h_html_a003c7af3c7c683a60a11ff0cde64365f"><div class="ttname"><a href="strfmt_8h.html#a003c7af3c7c683a60a11ff0cde64365f">cutl::fmt_timestamp_ms</a></div><div class="ttdeci">std::string fmt_timestamp_ms(uint64_t ms, bool local=true)</div><div class="ttdoc">Format a timestamp to a human-readable string.</div></div>
<div class="ttc" id="astrfmt_8h_html_a2e78cd1710a89e2f94f496bc47cd2d46"><div class="ttname"><a href="strfmt_8h.html#a2e78cd1710a89e2f94f496bc47cd2d46">cutl::fmt_uint</a></div><div class="ttdeci">std::string fmt_uint(uint64_t val, uint8_t width=0, char fill='0')</div><div class="ttdoc">Format uint64 value to a string with a given width and fill character.</div></div>
<div class="ttc" id="astrfmt_8h_html_a9a2a174ed592af8186835cd0da50a384"><div class="ttname"><a href="strfmt_8h.html#a9a2a174ed592af8186835cd0da50a384">cutl::fmt_filesize</a></div><div class="ttdeci">std::string fmt_filesize(uint64_t size, bool simplify=true, int precision=1)</div><div class="ttdoc">Format a file size to a human-readable string with a given precision.</div></div>
<div class="ttc" id="astrfmt_8h_html_a9b0341d973ea969f0d90e55f4a1771e1"><div class="ttname"><a href="strfmt_8h.html#a9b0341d973ea969f0d90e55f4a1771e1">cutl::fmt_double</a></div><div class="ttdeci">std::string fmt_double(double val, int precision=2)</div><div class="ttdoc">Format double value to a string with a given precision.</div></div>
</div><!-- fragment --><p>运行结果如下：</p>
<div class="fragment"><div class="line">[2024-05-18 16:01:03.477][I]]0x7ff844a9b100](cutl) [config.cpp:39:library_init] common_util library initialized, version:1.0.0</div>
<div class="line">fmt_uint: 00012</div>
<div class="line">fmt_double: 3.1416</div>
<div class="line">fmt_filesize: 32.8K</div>
<div class="line">current time(UTC time): 2024-05-18 08:01:03.478</div>
<div class="line">current time(local time): 2024-05-18 16:01:03.478</div>
</div><!-- fragment --><h1><a class="anchor" id="autotoc_md49"></a>
1.4. Demo</h1>
<p>src/usage_demo 有各个模块的使用示例。</p>
<div class="fragment"><div class="line">config.hpp      # 初始化配置</div>
<div class="line">datetime.hpp    # 日期时间类的使用</div>
<div class="line">filepath.hpp    # 文件路径类的使用</div>
<div class="line">fileutil.hpp    # 文件系统相关的操作</div>
<div class="line">strfmt.hpp      # 字符串格式化</div>
<div class="line">strutil.hpp     # 字符串相关的操作</div>
<div class="line">sysutil.hpp     # 系统调用相关的操作</div>
<div class="line">timecount.hpp   # 函数计时器类</div>
<div class="line">timeutil.hpp    # 时间相关的操作</div>
<div class="line">verutil.hpp     # 版本号相关的操作</div>
</div><!-- fragment --> </div></div><!-- contents -->
</div><!-- PageDoc -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated by&#160;<a href="https://www.doxygen.org/index.html"><img class="footer" src="doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.10.0
</small></address>
</body>
</html>
